package repository

import (
	"context"
	"go-pan/internal/model"
	"gorm.io/gorm"
)

type ShareRepository interface {
	Create(ctx context.Context, share *model.FileShare) error
	FindByToken(ctx context.Context, token string) (*model.FileShare, error)
}

type shareRepository struct {
	db *gorm.DB
}

func NewShareRepository(db *gorm.DB) ShareRepository {
	return &shareRepository{db: db}
}

func (r *shareRepository) Create(ctx context.Context, share *model.FileShare) error {
	return r.db.WithContext(ctx).Create(share).Error
}

func (r *shareRepository) FindByToken(ctx context.Context, token string) (*model.FileShare, error) {
	var s model.FileShare
	if err := r.db.WithContext(ctx).Where("token = ?", token).First(&s).Error; err != nil {
		return nil, err
	}
	return &s, nil
}
